package com.sigmatrix.h5.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.sigmatrix.h5.dao.PackStandardDao;
import com.sigmatrix.h5.dto.PackStandardData;
import com.sigmatrix.h5.entity.PackStandard;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.request.Request;
import com.sigmatrix.h5.service.PackStandardService;
import com.sigmatrix.h5.web.vo.PackStandardVo;
import com.sigmatrix.sc.jdbc.common.service.ServiceImpl;
/**
 * 
 *@ClassName: PackStandardServiceImpl
 *@Description: 包装规格
 *@author MH
 *@date 2017年9月27日 上午9:31:34
 */
@Service
public class PackStandardServiceImpl extends ServiceImpl<PackStandard, Integer> implements PackStandardService{
	
	@Autowired
	PackStandardDao packStandardDao;
	
	@Override
	public PackStandard findByProductIdAndLevel(Integer productId, Short level) {
		PackStandard vo = new PackStandard();
		vo.setProductId(productId);
		vo.setLevel(level);
		return packStandardDao.selectOne(vo);
	}
	
	/**
	 * (non-Javadoc)
	 *@Title: findById
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.PackStandardService#findById(java.lang.Integer)
	 *@author youjun
	 *@date 2017年6月26日 下午2:25:45
	 */
	@Override
	public PackStandardData findById(final Integer id, final String enterpriseCode) throws ServiceException {
		
		if(id==null) {
			throw new ServiceException("请指定主键");
		}
				
		final PackStandardData data = new PackStandardData();
		try {
			final PackStandard packStandard = packStandardDao.findById(id, enterpriseCode);
			if(packStandard!=null) {
				BeanUtils.copyProperties(packStandard, data);
				
			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		return data;
	}

	/**(non-Javadoc)
	 * @throws ServiceException 
	 *@Title: findPartColumnListByCondition
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.PackStandardService#findPartColumnListByCondition(com.sigmatrix.sc.handheld.inter.web.vo.PackStandardVo)
	 *@author youjun
	 *@date 2017年6月26日 下午2:25:13
	 */
	@Override
	public List<PackStandardData> findPartColumnListByCondition(final PackStandardVo packStandardVo) throws ServiceException {

		validateBasicParamsThrow(packStandardVo, false, false);
		try {
			PackStandard packStandard = new PackStandard();
			BeanUtils.copyProperties(packStandardVo, packStandard);
			return packStandardDao.findPartColumnListByCondition(packStandard, packStandardVo.getEnterpriseCode());
		}catch(DaoException e) {
			throw new ServiceException(e);
		}
	}
	
	/**
	 *@Title: validateBasicParamsThrow
	 *@Description: 验证基础参数,验证不通过抛出异常
	 *@return void
	 *@author youjun
	 *@date 2017年8月4日 下午5:31:21
	 *@param request
	 *@param isValidTerminalId
	 *@param isValidUserId
	 *@throws ServiceException
	 */
	protected void validateBasicParamsThrow(final Request request, final boolean isValidTerminalId, 
			final boolean isValidUserId) throws ServiceException {
		
		if(request==null) {
			throw new ServiceException("参数不能为空");
		}
		if(StringUtils.isBlank(request.getEnterpriseCode())) {
			throw new ServiceException("企业code不能为空");
		}
		if(isValidUserId && request.getUserId()==null) {
			throw new ServiceException("终端用户ID不能为空");
		}
	}
	

	/**
	 * (non-Javadoc)
	 *@Title: findListByCondition
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.PackStandardService#findListByCondition(com.sigmatrix.sc.handheld.inter.web.vo.PackStandardVo)
	 *@author youjun
	 *@date 2017年6月26日 下午2:25:32
	 */
	@Override
	public List<PackStandardData> findListByCondition(final PackStandardVo packStandardVo) throws ServiceException {

		if(packStandardVo==null) {
			throw new ServiceException("packStandardVo不能为空");
		}
		final List<PackStandardData> dataList = new ArrayList<PackStandardData>();
		try {
			final PackStandard packStandard = new PackStandard();
			if(packStandardVo!=null) {
				BeanUtils.copyProperties(packStandardVo, packStandard);
			}
			final List<PackStandard> list = packStandardDao.findListByCondition(packStandard, packStandardVo.getEnterpriseCode());
			if(!CollectionUtils.isEmpty(list)) {
				PackStandardData data = null;
				for(final PackStandard item:list) {
					data = new PackStandardData();
					BeanUtils.copyProperties(item, data);
					dataList.add(data);
				}
			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		return dataList;
	}

}
